package com.qdlc.p2p.dal.dao.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Query;

import com.qdlc.p2p.dal.dao.*;
import com.qdlc.p2p.dal.dto.*;
import org.springframework.stereotype.Repository;

import com.qdlc.p2p.common.util.BeanUtil;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.common.dao.jpa.BaseDaoImpl;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.common.model.jpa.QueryParam;
import com.qdlc.p2p.dal.common.model.jpa.SearchFilter;
import com.qdlc.p2p.dal.model.NewActivityRuleModel;

/**
 * Created by liaoxiangkai on 2015/11/12.
 */
@Repository("newActivityRuleDao")
public class NewActivityRuleDaoImpl extends BaseDaoImpl<NewActivityRule> implements NewActivityRuleDao {

    /**
     * 根据活动id进行查询
     *
     * @param activityId
     * @return
     */
    @SuppressWarnings("unchecked")
    @Override
    public List<NewActivityRule> findByActivityId(long activityId) {
        String jpql = "from NewActivityRule where newActivityPlan.id = :activityId";
        Query query = em.createQuery(jpql);
        query.setParameter("activityId", activityId);
        return query.getResultList();
    }

    /**
     * 后台获取活动规则列表
     *
     * @param model
     * @return
     */
    @Override
    public PageDataList<NewActivityRuleModel> list(NewActivityRuleModel model) {
        QueryParam param = QueryParam.getInstance().addPage(model.getPage(),
                model.getRows());
        if (StringUtil.isNotBlank(model.getSearchName())) {
            param.addParam("description", SearchFilter.Operators.LIKE, model.getSearchName());
        }
        PageDataList<NewActivityRule> dataList = this.findPageList(param);
        PageDataList<NewActivityRuleModel> modelDataList = new PageDataList<NewActivityRuleModel>();
        List<NewActivityRuleModel> list = new ArrayList<NewActivityRuleModel>();
        if (dataList.getList() != null && dataList.getList().size() > 0) {
            for (NewActivityRule entity : dataList.getList()) {
                NewActivityRuleModel newActivityRuleModel = NewActivityRuleModel.instance(entity);
                //1:红包 2:体验券 3:加息券 4:抽奖 5:电影票
                setRuleName(entity.getType(), entity.getRuleId(), newActivityRuleModel);
                //将活动名称设置给model
                if (null != entity.getNewActivityPlan())
                    newActivityRuleModel.setActivityName(entity.getNewActivityPlan().getName());
                //将修改人名字设置给model
                if (null != entity.getModifier())
                    newActivityRuleModel.setModifierName(entity.getModifier().getUserName());
                //把创建人名字设置给model
                if (null != entity.getCreator())
                    newActivityRuleModel.setCreatorName(entity.getCreator().getUserName());
                list.add(newActivityRuleModel);
            }
        }
        modelDataList.setPage(dataList.getPage());
        modelDataList.setList(list);
        return modelDataList;
    }

    /**
     * 更新活动规则状态
     *
     * @param model
     */
    @Override
    public int toggleStatus(NewActivityRuleModel model) {
        String jpql = "update NewActivityRule set status = ?1, modifier = ?2, modifyTime = ?3 where id = ?4";
        Query query = em.createQuery(jpql);
        query.setParameter(1, model.getStatus());
        query.setParameter(2, model.getModifier());
        query.setParameter(3, model.getModifyTime());
        query.setParameter(4, model.getId());
        return query.executeUpdate();
    }

    /**
     * 通过活动规则id查询
     *
     * @param id
     * @return
     */
    @Override
    public NewActivityRuleModel findByActivityRuleId(long id) {
        NewActivityRule newActivityRule = this.find(id);
        NewActivityRuleModel newActivityRuleModel = NewActivityRuleModel.instance(newActivityRule);
        if (null != newActivityRule) {
            //1:红包 2:体验券 3:加息券 4:抽奖 5:电影票
            setRuleName(newActivityRule.getType(), newActivityRule.getRuleId(), newActivityRuleModel);
            //将活动名称设置给model
            if (null != newActivityRule.getNewActivityPlan()){
                newActivityRuleModel.setActivityId(newActivityRule.getNewActivityPlan().getId());
                newActivityRuleModel.setActivityName(newActivityRule.getNewActivityPlan().getName());
            }
            //将修改人名字设置给model
            if (null != newActivityRule.getModifier())
                newActivityRuleModel.setModifierName(newActivityRule.getModifier().getUserName());
            //把创建人名字设置给model
            if (null != newActivityRule.getCreator())
                newActivityRuleModel.setCreatorName(newActivityRule.getCreator().getUserName());
        }
        return newActivityRuleModel;
    }

    /**
     * 给model设置规则名称
     *
     * @param ruleType
     * @param ruleId
     * @param model
     */
    private void setRuleName(int ruleType, long ruleId, NewActivityRuleModel model) {
        switch (ruleType) {
            case (1):
                RedEnvelopeDao redEnvelopeDao = (RedEnvelopeDao) BeanUtil.getBean("redEnvelopeDao");
                RedEnvelope redEnvelope = redEnvelopeDao.find(ruleId);
                if (null != redEnvelope)
                    model.setRuleName(redEnvelope.getName());
                break;
            case (2):
                ExperienceRuleDao experienceRuleDao = (ExperienceRuleDao) BeanUtil.getBean("experienceRuleDao");
                ExperienceRule experienceRule = experienceRuleDao.find(ruleId);
                if (null != experienceRule)
                    model.setRuleName(experienceRule.getName());
                break;
            case (3):
                RateRuleDao rateRuleDao = (RateRuleDao) BeanUtil.getBean("rateRuleDao");
                RateRule rateRule = rateRuleDao.find(ruleId);
                if (null != rateRule)
                    model.setRuleName(rateRule.getRuleName());
                break;
            case (4):
                AdditionalDrawLotteryRuleDao lotteryRuleDao = (AdditionalDrawLotteryRuleDao) BeanUtil.getBean("additionalDrawLotteryRuleDao");
                AdditionalDrawLotteryRule lotteryRule = lotteryRuleDao.find(ruleId);
                if (null != lotteryRule)
                    model.setRuleName(lotteryRule.getName());
                break;
            case (5):
                AdditionalMovieTicketRuleDao movieTicketRuleDao = (AdditionalMovieTicketRuleDao) BeanUtil.getBean("additionalMovieTicketRuleDao");
                AdditionalMovieTicketRule movieTicketRule = movieTicketRuleDao.find(ruleId);
                if (null != movieTicketRule)
                    model.setRuleName(movieTicketRule.getName());
                break;
            case (6):
                CashAwardRuleDao cashAwardRuleDao = (CashAwardRuleDao) BeanUtil.getBean("cashAwardRuleDao");
                CashAwardRule cashAwardRule = cashAwardRuleDao.find(ruleId);
                if (null != cashAwardRule)
                    model.setRuleName(cashAwardRule.getName());
                break;
            case (7):
            	WineCouponsDao wineCouponsDao = (WineCouponsDao) BeanUtil.getBean("wineCouponsDao");
            WineCoupons wineCoupons = wineCouponsDao.find(ruleId);
            if (null != wineCoupons)
            	model.setRuleName(wineCoupons.getName());
            break;
        }
    }
}
